<template>
	<el-form ref="loginForm" :model="ruleForm" :rules="rules" label-width="0" size="large">
		<el-form-item prop="user">
			<el-input v-model="ruleForm.user" prefix-icon="el-icon-user" clearable
					  :placeholder="$t('login.userPlaceholder')">
				<template #append>
					<el-select v-model="userType" style="width: 130px;">
						<el-option :label="$t('login.admin')" value="admin"></el-option>
						<el-option :label="$t('login.user')" value="user"></el-option>
					</el-select>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="ruleForm.password" prefix-icon="el-icon-lock" clearable show-password
					  :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn')
				}}
			</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
	export default {
		data() {
			return {
				userType: 'admin',
				ruleForm: {
					user: "admin",
					password: "admin",
					autologin: false
				},
				rules: {
					user: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					]
				},
				islogin: false,
				config: {
					lang: this.$TOOL.data.get('APP_LANG') || this.$CONFIG.LANG,
					theme: this.$TOOL.data.get('APP_THEME') || 'default'
				},
				lang: [
					{
						name: '简体中文',
						value: 'zh-cn',
					},
					{
						name: 'English',
						value: 'en',
					},
					{
						name: '日本語',
						value: 'ja',
					}
				]
			}
		},
		watch: {
			userType(val) {
				if (val == 'admin') {
					this.ruleForm.user = 'admin'
					this.ruleForm.password = 'admin'
				} else if (val == 'user') {
					this.ruleForm.user = 'user'
					this.ruleForm.password = 'user'
				}
			},
			'config.theme'(val) {
				document.body.setAttribute('data-theme', val)
				this.$TOOL.data.set("APP_THEME", val);
			},
			'config.lang'(val) {
				this.$i18n.locale = val
				this.$TOOL.data.set("APP_LANG", val);
			}
		},
		created: function () {
			this.$TOOL.data.remove("TOKEN")
			this.$TOOL.data.remove("USER_INFO")
			this.$TOOL.data.remove("MENU")
			this.$TOOL.data.remove("PERMISSIONS")
			this.$TOOL.data.remove("grid")
			this.$store.commit("clearViewTags")
			this.$store.commit("clearKeepLive")
			this.$store.commit("clearIframeList")
		},
		methods: {
			async login() {

				var validate = await this.$refs.loginForm.validate().catch(() => {
				})
				if (!validate) {
					return false
				}

				this.islogin = true
				var data = {
					username: this.ruleForm.user,
					password: this.$TOOL.crypto.MD5(this.ruleForm.password)
				}
				//获取token
				var user = await this.$API.auth.token.post(data)
				if (user.code == 200) {
					this.$TOOL.data.set("TOKEN", user.data.token)
					this.$TOOL.data.set("USER_INFO", user.data.userInfo)
				} else {
					this.islogin = false
					this.$message.warning(user.message)
					return false
				}
				//获取菜单
				var menu = null
				if (this.ruleForm.user == 'admin') {
					menu = await this.$API.system.menu.myMenus.get()
				} else {
					menu = await this.$API.demo.menu.get()
				}
				if (menu.code == 200) {
					if (menu.data.menu.length == 0) {
						this.islogin = false
						this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						return false
					}
					this.$TOOL.data.set("MENU", menu.data.menu)
					this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)
				} else {
					this.islogin = false
					this.$message.warning(menu.message)
					return false
				}

				this.$router.replace({
					path: '/'
				})
				this.$message.success("Login Success 登录成功")
				this.islogin = false
			},
			configTheme() {
				this.config.theme = this.config.theme == 'default' ? 'dark' : 'default'
			},
			configLang(command) {
				this.config.lang = command.value
			}
		}
	}
</script>

<style scoped>

</style>
